﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace GlyphMaster
{
	public class GlyphCollection : IEnumerable, IQueryable
	{
		private List<Glyph> _glyphs;
		public String collectionName { get; private set; }

		public GlyphCollection(String _collectionName)
		{
			collectionName = _collectionName;
			_glyphs = new List<Glyph>();
		}

		public void Add(Glyph glyph)
		{
			_glyphs.Add(glyph);
		}

		public void Remove(Glyph glyph)
		{
			if (_glyphs.Contains(glyph))
			{
				_glyphs.Remove(glyph);
			}
		}

		public int Count
		{
			get
			{
				return _glyphs.Count;
			}
		}

		public Glyph this[int indexer]
		{
			get { return _glyphs[indexer]; }
			set { _glyphs[indexer] = value; }
		}
		#region IEnumerable Members
		public IEnumerator GetEnumerator()
		{
			return new GlyphEnum(_glyphs);
		}
		#endregion

		#region IQueryable Members

		public Type ElementType
		{
			get { throw new NotImplementedException(); }
		}

		public System.Linq.Expressions.Expression Expression
		{
			get { throw new NotImplementedException(); }
		}

		public IQueryProvider Provider
		{
			get { throw new NotImplementedException(); }
		}

		#endregion
	}

	public class GlyphEnum : IEnumerator
	{
		public Glyph[] glyphs;

		// Enumerators are positioned before the first element
		// until the first MoveNext() call.
		int position = -1;

		public GlyphEnum(List<Glyph> list)
		{
			glyphs = list.ToArray();
		}

		#region IEnumerator Members

		public object Current
		{
			get
			{
				try
				{
					return glyphs[position];
				}
				catch (IndexOutOfRangeException)
				{
					throw new InvalidOperationException();
				}
			}
		}

		public bool MoveNext()
		{
			position++;
			return (position < glyphs.Length);
		}

		public void Reset()
		{
			position = -1;
		}

		#endregion
	}
}